﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DBQLTV;

namespace DAO
{
    public class PhieuMuonSachDAO
    {
        public bool InsertPhieuMuon(int MaDG, string NgayMuon, List<int> MaCuonSach)
        {
            using (DBQUANLYTHUVIENEntities entity = new DBQUANLYTHUVIENEntities())
            {
                DateTime ngayMuon = Convert.ToDateTime(NgayMuon);
                PHIEUMUONSACH phieuMuon_temp = entity.PHIEUMUONSACHes.FirstOrDefault(o => o.MaDG == MaDG && o.NgayMuon.Equals(ngayMuon));
                if (phieuMuon_temp != null)
                {
                    foreach (int int_temp in MaCuonSach)
                    {
                        CUONSACH cuonsach = entity.CUONSACHes.FirstOrDefault(o => o.MaCuonSach == int_temp);
                        if (cuonsach != null && !phieuMuon_temp.CUONSACHes.Contains(cuonsach))
                        {
                            cuonsach.TinhTrang = new CuonSachDAO().TT_DangMuon;
                            cuonsach.MaDocGia = MaDG;
                            cuonsach.NgayMuon = ngayMuon;
                            phieuMuon_temp.CUONSACHes.Add(cuonsach);
                        }
                    }
                }
                else
                {
                    phieuMuon_temp = new PHIEUMUONSACH()
                    {
                        MaDG = MaDG,
                        NgayMuon = ngayMuon
                    };

                    //Tạo liên kết
                    foreach (int int_temp in MaCuonSach)
                    {
                        CUONSACH cuonsach = entity.CUONSACHes.FirstOrDefault(o => o.MaCuonSach == int_temp);
                        if (cuonsach != null && !phieuMuon_temp.CUONSACHes.Contains(cuonsach))
                        {
                            cuonsach.TinhTrang = new CuonSachDAO().TT_DangMuon;
                            cuonsach.MaDocGia = MaDG;
                            cuonsach.NgayMuon = ngayMuon;
                            phieuMuon_temp.CUONSACHes.Add(cuonsach);
                        }
                    }
                    entity.PHIEUMUONSACHes.AddObject(phieuMuon_temp);
                }
                entity.SaveChanges();
                return true;
            }
        }
        public bool DeletePhieuMuonSach(int MaDG)
        {
            using (DBQUANLYTHUVIENEntities entity = new DBQUANLYTHUVIENEntities())
            {
                var listPhieuMuon = from temp in entity.PHIEUMUONSACHes where temp.MaDG == MaDG select temp;
                if (listPhieuMuon.Count() == 0) return false;
                foreach (PHIEUMUONSACH p in listPhieuMuon)
                {
                    p.CUONSACHes.Clear();
                    p.DOCGIA = null;
                    entity.PHIEUMUONSACHes.DeleteObject(p);
                }
                entity.SaveChanges();
                return true;
            }

        }
        public PHIEUMUONSACH GetPhieuMuonSach(int MaPMS)
        {
            using (DBQUANLYTHUVIENEntities entity = new DBQUANLYTHUVIENEntities())
            {
                PHIEUMUONSACH pms = entity.PHIEUMUONSACHes.FirstOrDefault(o => o.MaPhieuMuon == MaPMS);
                return pms;
            }
        }
    }
}
